package com.star.search.controller;

import com.star.entity.Page;
import com.star.search.pojo.SkuInfo;
import com.star.search.service.SearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.Set;

/*
			  ________   ___   ___        ___   ________   ___   ___        ___
	 ________|\   __  \_|\  \_|\  \______|\  \_|\   __  \_|\  \_|\  \______|\  \_________
	 \       \ \  \_\ /_\ \  \\ \  \     \ \  \\ \  \_\ /_\ \  \\ \  \     \ \  \        \
	  \       \ \   __  \\ \  \\ \  \     \ \  \\ \   __  \\ \  \\ \  \     \ \  \        \
	   \       \ \  \_\  \\ \  \\ \  \____ \ \  \\ \  \_\  \\ \  \\ \  \____ \ \  \        \
	    \       \ \_______\\ \__\\ \_______\\ \__\\ \_______\\ \__\\ \_______\\ \__\        \
	     \       \|_______| \|__| \|_______| \|__| \|_______| \|__| \|_______| \|__|         \
	 	  \___________________________________________________________________________________\

*/
@Controller
@RequestMapping("/search")
public class SearchController {
    @Autowired
    private SearchService searchService;


    @GetMapping("/list")
    public String list(@RequestParam Map<String,String> searchMap, Model model) {

        this.handleSearchMap(searchMap);
        Map resultMap = searchService.search(searchMap);
        model.addAttribute("result", resultMap);
        model.addAttribute("searchMap", searchMap);

        Page<SkuInfo> page = new Page<SkuInfo>(Long.parseLong(String.valueOf(resultMap.get("total"))),
                Integer.parseInt(String.valueOf(resultMap.get("pageNum"))),
                Page.pageSize);

        model.addAttribute("page", page);
//        page.setCurrentpage(Long.parseLong(searchMap.get("pageNum")),Long.parseLong(resultMap.get("total").toString()),Long.parseLong(searchMap.get("pageSize")));
        //瓶装搜索路径
        StringBuilder url = new StringBuilder("/search/list");
        if (searchMap != null && searchMap.size() > 0) {
            //searchMap中有查询条件，进行拼装
            url.append("?");
            for (String paramKey : searchMap.keySet()) {
                //如果不是 排序规则  排序字段 PageNum PageSize 就把参数进行拼装
                if (!paramKey.equals("sortRule") && !paramKey.equals("sortField") && !paramKey.equals("pageNum") && !paramKey.equals("pageSize")) {

                    url.append(paramKey).append("=").append(searchMap.get(paramKey)).append("&");
                }
            }

            String urlString = url.toString();
            //处理最后一个 &
            urlString = urlString.substring(0, url.length() - 1);

            model.addAttribute("url", urlString);
        } else {
            //入股没有搜索数据
            model.addAttribute("url", url);
        }
        return "search";
    }

    /**
     * 条件查询
     * @param searchMap
     * @return
     */
    @GetMapping
    @ResponseBody
    public Map search(@RequestParam Map<String,String> searchMap) {
        //特殊符号处理
        this.handleSearchMap(searchMap);
        Map search = searchService.search(searchMap);
        return search;
    }

    /**
     * 处理搜索路径
     * @param searchMap
     */
    private void handleSearchMap(Map<String, String> searchMap) {
        Set<Map.Entry<String, String>> entries = searchMap.entrySet();
        for (Map.Entry<String, String> entry : entries) {
            if (entry.getKey().startsWith("spec_")) {
                searchMap.put(entry.getKey(), entry.getValue().replace("+", "%2B"));
            }
        }
    }
}
